var selectTree;
Ext.define('AM.view.tree.Trees', {
    alias: 'widget.treepane',
    id: 'treepane',
    extend: 'Ext.tree.Panel',
    store: 'StoreTree',
    rootVisible: false,
    useArrows: true,
    lines: true,
    split: true,
    //style: 'background:transparent',
    //bodyStyle: 'background:transparent',
    
    initComponent: function(){
    	this.tbar= [
                { 
                	xtype: 'button', 
                	text: 'Add',
                	//action: 'add',
                	icon:'images/add.png',
                	handler: this.addTree,
                	scope: this
                	},
                { 
                	xtype: 'button', 
                	text: 'Edit',
                	action: 'edit',
                	icon: 'images/edit.png',
                	handler: this.editTree,
                	scope: this
                },
                { 
                	xtype: 'button', 
                	text: 'Delete',
                	action: 'del',
                	icon: 'images/delete.png',
                	handler: this.deleteTree,
                	scope: this
                }
            ],
            this.bbar= [{
                text: 'Versiunea 1.3'
            }],
            this.columns= [{
            	xtype: 'treecolumn', //this is so we know which column will show the tree
            	text: 'Proiect',
            	flex: 2,
            	sortable: true,
            	dataIndex: 'task',
            }/*,{
            	text: 'New',
            	flex: 1,
            	dataIndex: 'user',
            	sortable: true
            }*/],
            /*this.viewConfig = {
                stripeRows: true,
                listeners: {
                    itemcontextmenu: function(view, rec, node, index, e) {
                        e.stopEvent();
                        var contextMenu = Ext.create('Ext.menu.Menu', {
                            width: 100,
                            id:'mm',
                            height: 100,
                            margin: '0 0 10 0',
                            //floating: false,  
                            items: [{ 
                                text: 'regular item 1',
                                //action: 'i',
                                //lisener: this.test1,
                            	//scope: this
                            },{
                                text: 'regular item 2',
                                //action: 'ii',
                                handler:this.test2,
                            	scope: this
                            },{
                                text: 'regular item 3',
                                //action: 'iii',
                                handler:this.test3,
                            	//scope: this
                            }]
                        });
                        //var contextMenu = Ext.widget('contextmeniu');
                        contextMenu.showAt(e.getXY());
                        return false;
                    }
                }
            },*/
    	
    	  //this.store = Ext.data.StoreManager.lookup(this.store);
    	this.callParent(arguments);
    },
    test1: function (obj,e) {
    	console.log('Test 1');
		    
	},
	test2: function (obj,e) {
    	console.log('Test 2');
		    
	},
	test3: function (obj,e) {
    	console.log('Test 3');
		    
	},
    addTree: function (obj,e) {
//    	console.log('Add new Tree...');
    	var vnode = this.getSelectionModel().getSelection();
    	Ext.getStore('StoreComboSubpro').load({params: {storeTip: 'comboSubPro', acction: 'combosubpro', UserID: UserID},
    	callback: function() {
    		var view = Ext.widget('addtree');
        	if(vnode[0]){
                if(vnode[0].data.iconCls == 'task'){
                    var pro = vnode[0].parentNode.parentNode.data.iddd;
                    var subpro = vnode[0].parentNode.data.idd;
                    var cild = vnode[0].data.id;
                    view.down('#pro').setValue(pro);
                    view.down('#subpro').setValue(subpro);
                    view.down('#cild').setValue(cild);
                } else if(vnode[0].data.iconCls == 'task-folder'){
                    var pro = vnode[0].parentNode.data.iddd;
                    var subpro = vnode[0].data.idd;
                    view.down('#pro').setValue(pro);
                    view.down('#subpro').setValue(subpro);
                } else if(vnode[0].data.iconCls == 'task-folder-root'){
                    var pro = vnode[0].data.iddd;
                    view.down('#pro').setValue(pro);
                }
        	}
    	}});  
	},
    deleteTree: function () {
//    	console.log('Delete Tree');
    	var node = this.getView().getSelectionModel().getSelection();
		//console.log(node);
		if (node[0].data.iconCls == 'task') {
//			this.store.remove(this, node[0].data.id);
			Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'delChild',
                    acction: 'delcild',
                    id: node[0].data.id,
                    UserID: UserID
                },
                success: function(response){
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });
			/*Ext.getStore('StoreInsert').load({
		  		params: {
		  		    storeTip: 'delChild',
		  			acction: 'delcild',
		  			id: node[0].data.id
		  		},
		  		callback : function() {
		  			Ext.getStore('StoreTree').load({params: { acction: 'tree', UserID: UserID}});
		  		}
		  	});*/
		} else if (node[0].data.iconCls == 'task-folder') {
            Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'delSubPro',
                    acction: 'delsubpro',
                    id: node[0].data.idd,
                    UserID: UserID
                },
                success: function(response){
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });
            /*Ext.getStore('StoreInsert').load({
                params: {
                    storeTip: 'delSubPro',
                    acction: 'delsubpro',
                    id: node[0].data.idd
                },
                callback : function() {
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });*/
		} else if (node[0].data.iconCls == 'task-folder-root') {
		    Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'delPro',
                    acction: 'delpro',
                    id: node[0].data.iddd,
                    UserID: UserID
                },
                success: function(response){
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });
            /*Ext.getStore('StoreInsert').load({
                params: {
                    storeTip: 'delPro',
                    acction : 'delpro',
                    id:node[0].data.iddd
                },
                callback : function() {
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });*/
		}
	},
	editTree: function (obj,e) {
//		console.log('Edit Tree...');
    	var view = Ext.widget('edittree');
    	var vnode = this.getSelectionModel().getSelection();
    	selectTree = vnode;
        var edit = vnode[0].data.task;
    	if(vnode[0]){
            if(vnode[0].data.iconCls == 'task'){
                view.down('#edit').setValue(edit);
            }
            else if(vnode[0].data.iconCls == 'task-folder'){
                view.down('#edit').setValue(edit);
            }
            else if(vnode[0].data.iconCls == 'task-folder-root'){
                view.down('#edit').setValue(edit);
            }
    	}
	},
});